package cn.xspace.wnx.util;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.xspace.android.xafcommon.util.ClassUtil;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.RequestConstant;

import java.util.ArrayList;

import cn.xspace.library.bean.OrgDeptUserBean;
import cn.xspace.library.util.BldNavigation;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.wnx.R;
import cn.xspace.wnx.activity.ACT_ChartImageBrowse;
import cn.xspace.wnx.activity.ChatActivity;
import cn.xspace.wnx.activity.ChatInfoActivity;
import cn.xspace.wnx.activity.ChatRecordActivity;
import cn.xspace.wnx.activity.GroupListActivity;
import cn.xspace.wnx.activity.GroupMembersListActivity;
import cn.xspace.wnx.activity.MsgForwardActivity;
import cn.xspace.wnx.activity.MyFriendActivity;
import cn.xspace.wnx.activity.OrganizationActivity;
import cn.xspace.wnx.activity.OrganizationInfoActivity;
import cn.xspace.wnx.activity.OrganizationPostInfoActivity;
import cn.xspace.wnx.activity.PersonCardActivity;
import cn.xspace.wnx.activity.SelectMemberActivity;
import cn.xspace.wnx.activity.ServiceObjActivity;
import cn.xspace.wnx.activity.UserSearchResultActivity;
import cn.xspace.wnx.activity.WnxOrgMessageListActivity;
import cn.xspace.wnx.activity.WnxWebViewAct;
import cn.xspace.wnx.activity.WnxorgListActivity;
import cn.xspace.wnx.entity.WNXBean;

//import cn.xspace.wnx.activity.ACT_ChartImageBrowse;
//import cn.xspace.wnx.activity.GroupMembersListActivity;
//import cn.xspace.wnx.activity.ImageLargerActivity;

/**
 * 跳转导航
 * Created by liuchen on 2016/10/13.
 */
public class Navigator {

    /**
     * 跳转到聊天界面
     *
     * @param context
     * @param bundle
     */
    public static void startChatActivity(Context context, Bundle bundle) {

//        Map<String, Object> finishMap = new HashMap<>();
//        finishMap.put(EventBusConstant.TYPE, EventBusConstant.ChatFinish);
//        EventBus.getDefault().post(finishMap);
        Intent intent = new Intent(context, ChatActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(BldNavigation.KEY_BUNDLER, bundle);
//        intent.putExtra(SENDER_ID, userId);
//        intent.putExtra(CHAT_TYPE, chatType);
//        intent.putExtra(CHAT_NAME, name);
//        intent.putExtra(CHAT_SENDER_LOGO, senderLogo);
        context.startActivity(intent);
    }

    //机构职能信息页面
    public static void startOrgPostInfoActivity(Context context, String id) {
        Intent intent = new Intent(context, OrganizationPostInfoActivity.class);
        intent.putExtra(RequestConstant.KEY_ORG_ID, id);
        context.startActivity(intent);
    }


    //机构详情页面仅需要机构id的
    public static void startOrgInfoActivity(Context context, String id) {
        Intent intent = new Intent(context, OrganizationInfoActivity.class);
        intent.putExtra(RequestConstant.KEY_ORG_ID, id);
        context.startActivity(intent);
    }

    public static void startChatRecordActivity(Context context, Bundle bundle) {
        Intent intent = new Intent(context, ChatRecordActivity.class);
        intent.putExtra(BldNavigation.KEY_BUNDLER, bundle);
//        intent.putExtra(SENDER_ID, userId);
//        intent.putExtra(CHAT_TYPE, chatType);
//        intent.putExtra(CHAT_NAME, name);
//        intent.putExtra(CHAT_SENDER_LOGO, senderLogo);
        context.startActivity(intent);
    }

    /**
     * 查看大图
     *
     * @param mContext
     * @param mBundle
     */
    public static void showImageLarerActivity(Context mContext, Bundle mBundle) {
        Intent mIntent = new Intent(mContext, ACT_ChartImageBrowse.class);
        mIntent.putExtra(BldNavigation.KEY_BUNDLER, mBundle);
        mContext.startActivity(mIntent);
    }

    /**
     * 通讯录界面上方的5个头条目
     *
     * @param mContext
     * @param id       那个条目的Id
     * @param mBundle  如果是转发就传递参数
     */
    public static void startHeaderActivity(Context mContext, int id, Bundle mBundle) {
        try {
            Intent mIntent = new Intent();
            if (id == R.id.header_organization) { //组织机构
                mIntent = new Intent(mContext, OrganizationActivity.class);
            } else if (id == R.id.header_service_obj) { //服务对象
                mIntent = new Intent(mContext, ServiceObjActivity.class);//TODO 现网打包暂不开放
//                T.showShort(mContext, mContext.getResources().getString(R.string.not_open_for_now));
//                return;
            } else if (id == R.id.header_weinong_orgnize) { //微农机构的条目已经隐藏
                mIntent = new Intent(mContext, WnxorgListActivity.class);
            } else if (id == R.id.header_my_friend) { //我的好友
                mIntent = new Intent(mContext, MyFriendActivity.class);
            } else if (id == R.id.header_my_group) { //我的群组
                mIntent = new Intent(mContext, GroupListActivity.class);
            } else if (id == R.id.edit_search || id == R.id.relative_search) { //搜索
                mIntent = new Intent(mContext, UserSearchResultActivity.class);
            }
            mIntent.putExtra(BldNavigation.KEY_BUNDLER, mBundle);
            mContext.startActivity(mIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


//    /**
//     * 跳转到用户搜索页面
//     *
//     * @param mContext
//     */
//    public static void startUserSearchActivity(Context mContext) {
//        Intent mIntent = new Intent(mContext, UserSearchResultActivity.class);
//        mContext.startActivity(mIntent);
//    }

//    /**
//     * 地图views
//     *
//     * @param mContext
//     * @param lat
//     * @param lng
//     * @param address
//     */
//    public static void startMapViewActivity(Context mContext, double lat, double lng, String address) {
//        Intent mIntent = new Intent(mContext, MapLocationGetActivity.class);
//        mIntent.putExtra(IntentConstant.LATITUDE, lat);
//        mIntent.putExtra(IntentConstant.LONGITUDE, lng);
//        mIntent.putExtra(IntentConstant.ADDRESS, address);
//        mContext.startActivity(mIntent);
//    }

    /**
     * 跳转个人名片页面
     *
     * @param mContext
     * @param user_id
     */
    public static void startPersonCardActivity(Context mContext, String user_id) {
        Intent mIntent = new Intent(mContext, PersonCardActivity.class);
        mIntent.putExtra(PersonCardActivity.FRIEND_ID, user_id);
        mContext.startActivity(mIntent);
    }

    /**
     * 开启新的会话
     *
     * @param mContext
     */
    public static void startNewChatActivity(Context mContext, Bundle mBundle) {
        Intent mIntent = new Intent(mContext, SelectMemberActivity.class);
        if (mBundle == null) {
            mBundle = new Bundle();
            mBundle.putString(ChatInfoActivity.KEY_MEMBER_OPERATE, ChatInfoActivity.MEMBER_ADD);
            String user_id = MyInfoUtil.getMyUserId();
            mBundle.putString(ChatInfoActivity.GROUP_OWNER, user_id);
            ArrayList<OrgDeptUserBean> list = new ArrayList<>();
            OrgDeptUserBean mineBean = new OrgDeptUserBean();
            mineBean.user_id = user_id;
            mineBean.user_name = MyInfoUtil.getName(mContext);
            mineBean.user_logo = MyInfoUtil.getMyLogo(mContext);
            list.add(mineBean);
            mBundle.putSerializable(SelectMemberActivity.SELECTED_MEMBERS, list);
        }
        mIntent.putExtra(BldNavigation.KEY_BUNDLER, mBundle);
        mContext.startActivity(mIntent);
    }

    /**
     * 跳往转发
     *
     * @param mContext
     * @param mBundle
     */
    public static void startForwardActivity(Context mContext, Bundle mBundle) {
        Intent mIntent = new Intent(mContext, MsgForwardActivity.class);
        mIntent.putExtra(BldNavigation.KEY_BUNDLER, mBundle);
        mContext.startActivity(mIntent);
    }


    /**
     * 跳转到聊天信息界面
     */
    public static void startChatMessageActivity(Context mContext, int chatType, String id, int groupType, int isQuit) {
        LogUtil.d("startChatMessageActivity() called with: mContext = [" + mContext + "], chatType = [" + chatType +
                "], id = [" + id + "]");
        Intent mIntent = new Intent(mContext, ChatInfoActivity.class);
        mIntent.putExtra(BldNavigation.CHAT_TYPE, chatType);
        mIntent.putExtra(BldNavigation.SENDER_ID, id);
        mIntent.putExtra(BldNavigation.GROUP_TYPE, groupType);
        mIntent.putExtra(BldNavigation.IS_QUIT, isQuit);
        mContext.startActivity(mIntent);
    }

    /**
     * 跳往群组成员列表页面
     *
     * @param mContext
     * @param dataBundle
     */
    public static void startGroupMemberListActivity(Context mContext, Bundle dataBundle) {
        Intent mIntent = new Intent(mContext, GroupMembersListActivity.class);
        mIntent.putExtra(BldNavigation.KEY_BUNDLER, dataBundle);
        mContext.startActivity(mIntent);
    }

    public static void startWnxListActivity(Context mContext, WNXBean bean) {
        Intent mIntent = new Intent(mContext, WnxOrgMessageListActivity.class);
        mIntent.putExtra(WnxOrgMessageListActivity.WNXBEAN, bean);
        mContext.startActivity(mIntent);
    }

    public static void startWnxListActivity(Context mContext, Bundle dataBundle) {
        Intent mIntent = new Intent(mContext, WnxOrgMessageListActivity.class);
        mIntent.putExtra(BldNavigation.KEY_BUNDLER, dataBundle);
        mContext.startActivity(mIntent);
    }

    /**
     * 跳转到个人中心的webview页面
     *
     * @param mContext
     * @param url
     * @param strExTitle
     */
    public static void startExtWebView(Context mContext, String url, String strExTitle) {
        Class activityClass = null;
        try {
            activityClass = Class.forName(ClassUtil.BROWSER_CENTER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(mContext, activityClass);// 你想去的activity(exp:Temp)
        if (!TextUtils.isEmpty(url)) {
            intent.putExtra(Constants.TITITL, strExTitle);
            intent.putExtra(Constants.WEBVIEURL, url);
        }
        mContext.startActivity(intent);
    }

    public static void startCollectActivity(Context mContext, String webpage_title, String favorite_url, String
            sender_id, String msg_org, int msg_type, String msg_deptid) {
        Class activityClass = null;
        try {
            activityClass = Class.forName(ClassUtil.MY_FAVORITE_MANAGER_ACTIVITY);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(mContext, activityClass);// 你想去的activity(exp:Temp)
        intent.putExtra("webpage_title", webpage_title);
        intent.putExtra("favorite_url", favorite_url);
        intent.putExtra("sender_id", sender_id);
        intent.putExtra("msg_type", msg_type);
        intent.putExtra("msg_org", msg_org);
        intent.putExtra("msg_deptid", msg_deptid);
        mContext.startActivity(intent);
    }

//    public static void startPlatformWebView(Context mContext, String url, String strExTitle, String org_id) {
//        Class activityClass = null;
//        try {
//            activityClass = Class.forName(ClassUtil.PLATFORM_WEBVIEWACT);
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//        Intent intent = new Intent(mContext, activityClass);// 你想去的activity(exp:Temp)
//        if (!TextUtils.isEmpty(url)) {
//            intent.putExtra(Constants.TITITL, strExTitle);
//            intent.putExtra(Constants.WEBVIEURL, url);
//        }
//        if (!TextUtils.isEmpty(org_id)) {
//            intent.putExtra(RequestConstant.KEY_ORG_ID, org_id);
//        }
//        mContext.startActivity(intent);
//    }

    public static void startWnxWebView(Context mContext, String url, String strExTitle, String org_id, int type) {
//        Class activityClass = null;
//        try {
//            activityClass = Class.forName(ClassUtil.WNX_WEBVIEWACT);
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
        Intent intent = new Intent(mContext, WnxWebViewAct.class);// 你想去的activity(exp:Temp)
        if (!TextUtils.isEmpty(url)) {
            intent.putExtra(Constants.TITITL, strExTitle);
            intent.putExtra(Constants.WEBVIEURL, url);
        }
        if (!TextUtils.isEmpty(org_id)) {
            intent.putExtra(RequestConstant.KEY_ORG_ID, org_id);
        }
        intent.putExtra("type", type);
        mContext.startActivity(intent);
    }
}
